package com.it.you.base.stream;

import com.it.you.pojo.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 处理重复key办法   (v1, v2) -> v2   使用第二个的key和value作为结果
 */
public class StreamTest2 {
    public static void main(String[] args) {
        List<String> cartDetailIdList = new ArrayList<>();
        cartDetailIdList.add("10");
        cartDetailIdList.add("20");
        cartDetailIdList.add("60");
        cartDetailIdList.add("40");
        int[] i = {1};
        Map<String, Integer> cartDetailMap = cartDetailIdList.stream()
                .collect(Collectors.toMap(key -> key, value-> i[0]++, (v1, v2) -> v1));
        System.out.println(cartDetailMap);

        //list为空的时候 调用stream()方法不会报错
        List<Student> students = new ArrayList<>();
        List<String> nameList = students.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println(nameList);
        if (nameList.contains("2")) {
            System.out.println("包含2");
        }else {
            System.out.println("不包含2");
        }

        Map<String, Student> map = students.stream().collect(Collectors.toMap(Student::getName, Function.identity()));//返回的map不是null
        System.out.println(map);
        System.out.println(map.get("you"));

    }

}
